package com.king.wms.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.king.wms.pojo.Loc;
import com.king.wms.pojo.Receipt;
import com.king.wms.pojo.Sku;
import com.king.wms.pojo.Storer;
import com.king.wms.service.LocService;
import com.king.wms.service.ReceiptService;
import com.king.wms.service.SkuService;
import com.king.wms.service.StorerService;

@Controller
@RequestMapping(value = "/receipt")
public class ReceiptController {
	@Autowired
	private ReceiptService receiptService;
	@Autowired
	private SkuService skuService;
	@Autowired
	private LocService locService;
	@Autowired
	private StorerService storerService;

	@RequestMapping(value = "/findAll")
	public void findAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		List<Loc> list_loc = locService.findAll();
		List<Storer> list_storer = storerService.findAll();
		List<Sku> list_sku = skuService.findAll();
		List<Receipt> list_received = receiptService.findReceived();
		List<Receipt> list_unreceived = receiptService.findUnReceived();

		request.setAttribute("list_received", list_received);
		request.setAttribute("list_unreceived", list_unreceived);
		request.setAttribute("list_loc", list_loc);
		request.setAttribute("list_storer", list_storer);
		request.setAttribute("list_sku", list_sku);
		
		request.getRequestDispatcher("/jsp/receipt/receipt.jsp").forward(
				request, response);
	}
	@RequestMapping(value = "/findAll1")
	public void findAll1(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		List<Loc> list_loc = locService.findAll();
		String s1 = "['库位数量',";
		String s2 = "['库位区域',";
		for(Loc loc:list_loc){
			s1 +=loc.getStacklimit()+",";
			s2 += "'"+loc.getRegion()+"',";
		}
		s1 = s1.substring(0,s1.length())+"]";
		s2 = s2.substring(0,s2.length())+"]";

		request.setAttribute("s1", s1);
		request.setAttribute("s2", s2);
		request.getRequestDispatcher("/jsp/loc/jinhuodan.jsp").forward(
				request, response);
	}
	@RequestMapping(value = "/findReceiptdetailByReceipt_id/{receipt_id}")
	public void findReceiptdetailByReceipt_id(@PathVariable("receipt_id") Integer receipt_id,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Receipt receipt = receiptService.findReceiptByPrimaryKey(receipt_id);
		
		Loc loc = locService.findLocByLoc(receipt.getLoc_id());
		Sku sku = skuService.findSkuBySku_id(receipt.getSku_id());
		Storer storer = storerService.findStorerByStorerkey(receipt.getStorer_id());
		
		
		request.setAttribute("loc", loc);
		request.setAttribute("sku", sku);
		request.setAttribute("storer", storer);
		request.setAttribute("receipt", receipt);
		
		request.getRequestDispatcher("/jsp/receipt/receiptDetails.jsp").forward(
				request, response);
		
	}
	
	
	@RequestMapping(value = "/updateStatus/{receipt_id}")
	public String updateStatus(@PathVariable("receipt_id") Integer receipt_id,
			HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
		
		receiptService.updateStatus(receipt_id);
		
		return "redirect:/receipt/findAll";
	}
	
	@RequestMapping(value = "/update")
	public String update(Receipt receipt,
			HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
		
		
		String message = receiptService.update(receipt);
		
		if( "success".equals(message) ){
			return "redirect:/receipt/findAll";
		}else{
			
			request.setAttribute("message", message);
			
			return "error";
		}
		
	}
	
	@RequestMapping("/toUpdateLocPage/{receipt_id}")
	public void toUpdateLocPage(@PathVariable("receipt_id") Integer receipt_id,
			HttpServletResponse response, HttpServletRequest request)
			throws ServletException, IOException {
		
		Receipt receipt = receiptService.findReceiptByReceipt_id(receipt_id);
		
		List<Loc> list_loc = locService.findAll();
		
		List<Storer> list_storer = storerService.findAll();
		
		List<Sku> list_sku = skuService.findAll();

		request.setAttribute("list_loc", list_loc);
		request.setAttribute("list_storer", list_storer);
		request.setAttribute("list_sku", list_sku);
		
		
		request.setAttribute("receipt", receipt);
		
		System.out.println("====================="+receipt);
		request.getRequestDispatcher("/jsp/receipt/updateReceipt.jsp").forward(request,
				response);

	}

	
	@RequestMapping(value = "/insert")
	public String insert(Receipt receipt,
			HttpServletResponse response, HttpServletRequest request)
					throws ServletException, IOException {
		
		String message = receiptService.insert(receipt);
		
		if( "success".equals(message) ){
			return "redirect:/receipt/findAll";
		}else{
			
			request.setAttribute("message", message);
			
			return "error";
		}
		
		
	}
	@RequestMapping(value = "/delete/{receipt_id}")
	public String delete(@PathVariable Integer receipt_id) {
		receiptService.delete(receipt_id);
		return "redirect:/receipt/findAll";
	}
	
}
